﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using Business;

namespace Data
{

    public class DataPresenca
    {
        public DataPresenca()
        {

        }

        public bool insertPresenca(Presenca p)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "INSERT INTO EventoPessoas(Evento_idEvento,Utilizador_idUtilizador) VALUES " + " (" + p.getPresencaEventoID() + "," + p.getPresencaUtilizadorID() + ")";
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return value;
        }

        public bool removePresenca(Presenca p)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "DELETE FROM EventoPessoas WHERE Evento_idEvento=" + p.getPresencaEventoID() + " AND Utilizador_idUtilizador=" + p.getPresencaUtilizadorID();
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return value;
        }

        public bool getPresencaUser(int userID,int eventID)
        {
            bool res = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from EventoPessoas where Utilizador_idUtilizador=" + userID + " AND Evento_idEvento=" + eventID;
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    if (reader.Read())
                        res = true;
                    else
                        res = false;
                }
                catch (Exception ex)
                {
                    //return false;
                }
              
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return res;
        }

        public ListaPresenca getListaPresencas(int eventoID)
        {
            Presenca pres = null;
            Connect c = new Connect();
            ListaPresenca lp = new ListaPresenca();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from EventoPessoas where Evento_idEvento=" + eventoID;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    pres = new Presenca(Convert.ToInt16(reader[0]), Convert.ToInt16(reader[1]));
                    lp.addPresenca(pres);
                }
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return lp;

        }
    }

}